package com.zm.test;

import com.zm.dao.IUserDao;
import com.zm.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MyBatisAnnoTestUser {
    private SqlSessionFactory factory;
    private SqlSession session;
    private IUserDao userDao;
    private InputStream in;

    @Before
    public void init() throws IOException {
        //1.获取字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");

        //2.根据字节输入流构建SqlSessionFactory
        factory=new SqlSessionFactoryBuilder().build(in);

        //3.根据SqlSessionFactory生产一个SqlSession
        session = factory.openSession();

        //4.使用SqlSession获取dao的代理对象
        userDao = session.getMapper(IUserDao.class);

    }

/**
     * 保存方法
     */


    @Test
    public void testSaveUser(){
        User user=new User();
        //与实体类属性名保持一致
        user.setUserName("贾贵生");
        user.setUserAddress("天津市蓟州区");
        user.setUserSex("男");
        userDao.saveUser(user);
    }


/**
     * 更新方法
     */

    @Test
    public void testUpDate(){
        User user=new User();
        user.setUserId(49);
        user.setUserName("贾贵生");
        user.setUserAddress("蓟州区下窝头乡");
        user.setUserSex("男");
        user.setUserBirthday(new Date());
        userDao.upDate(user);
    }

/**
     * 删除方法
     */


    @Test
    public void testDeleteUser(){
        userDao.deleteUser(41);
    }


/**
     * 查询总数
     */

    @Test
    public void testFindTotal(){
        int Total =userDao.findTotalUser();
        System.out.println(Total);
    }


    //====================================查询方法==============================================

    /**
     * 测试基于注解的mybatis使用
     * 查询所有人
     *
     */
    @Test
    public void testFindAll() {
        List<User> users=userDao.findAll();
        for (User user :users){
            System.out.println("==================================每个用户及其账户信息=====================================");
            System.out.println(user);
            System.out.println("------------------------------------------------------------------------------------");
            System.out.println(user.getAccounts());
            System.out.println();
            System.out.println();
        }

    }

    /**
     * 根据id查询用户信息
     */

    @Test
    public void testFindById(){
     User user= userDao.findById(41);
        System.out.println(user);
    }

    /**
     * 根据用户名进行模糊查询
     */

    @Test
    public void testFindByName(){
        List<User> users =userDao.findUserByName("%"+"王"+"%");
        for (User user : users){
            System.out.println(user);
        }
    }



    @After
    public void destroy() throws IOException {
        //5.事务提交
        session.commit();
        //6.释放资源
        session.close();
        in.close();
    }
}
